package com.gmrz.webauthn.db.dao;

import com.gmrz.uaf.db.DAOException;
import com.gmrz.util.db.DBUtil;
import com.gmrz.webauthn.protocol.v1.processor.WebAuthnErrorCode;
import com.gmrz.webauthn.protocol.v1.schema.Facets;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//暂时没用
public class BaseFacetsDAO extends Dialect implements FacetsDAO {
    private static final Logger LOG = LogManager.getLogger(BaseFacetsDAO.class);

    private static final String _SQL_SELECT_ALL = "select tc_id,config_cname,config_ename,config_value,data_type,regex,error_msg,createts,tenant_id,trans_type from t_tenant_config where 1 = 1 ";


    private Connection conn_;

    @AssistedInject
    public BaseFacetsDAO(@Assisted Connection conn_) {
        this.conn_ = conn_;
    }

    @Override
    public List<Facets> find(Facets facets) throws DAOException {
        StringBuilder sql = new StringBuilder(_SQL_SELECT_ALL);
        PreparedStatement s = null;
        ResultSet rs = null;
        List<Facets> result = new ArrayList<Facets>();


        try {

            List<Object> paras = null;
            if (facets != null) {

                paras = new ArrayList<Object>();

                if (StringUtils.isNotBlank(facets.getFacetId())) {
                    sql.append(" and facet_id = ?");
                    paras.add(facets.getFacetId());
                }

                if (StringUtils.isNotBlank(facets.getFacetName())) {
                    sql.append(" and facet_cname like ?");
                    paras.add(facets.getFacetName());
                }

                if (StringUtils.isNotBlank(facets.getFacetType())) {
                    sql.append(" and facet_type = ?");
                    paras.add(facets.getFacetType());
                }

                if (StringUtils.isNotBlank(facets.getAuthCode())) {
                    sql.append(" and auth_code = ?");
                    paras.add(facets.getAuthCode());
                }

                if (StringUtils.isNotBlank(facets.getTenantId())) {
                    sql.append(" and tenant_id = ?");
                    paras.add(facets.getTenantId());
                }
            }

            s = this.conn_.prepareStatement(sql.toString());

            if (paras != null && !paras.isEmpty()) {
                fillStatement(s, paras);
            }
            rs = s.executeQuery();

            while (rs.next()) {
                result.add(readResultSet(rs));
            }
        } catch (SQLException sqle) {
            LOG.error("The following SQL statement failed to execute:\n {}", sql);
            throw new DAOException(WebAuthnErrorCode.ERROR_DATABASE_READ_ERROR, sqle);
        } finally {
            DBUtil.close(rs);
            DBUtil.close(s);
        }

        return result;
    }

    private Facets readResultSet(ResultSet rs) throws DAOException {
        Facets facets = new Facets();
        try {
            int offset = 1;
            facets.setFacetId(rs.getString(offset++));
            facets.setFacetName(rs.getString(offset++));
            facets.setFacetType(rs.getString(offset++));
            facets.setAuthCode(rs.getString(offset++));
            facets.setCreatets(rs.getTimestamp(offset++));
            facets.setUpdatets(rs.getTimestamp(offset++));
            facets.setTenantId(rs.getString(offset));

            return facets;

        } catch (SQLException sqle) {
            LOG.error("Failed to read the Tenant Config Data", sqle);
            throw new DAOException(WebAuthnErrorCode.ERROR_DATABASE_READ_ERROR, sqle);
        }
    }
}
